#include <time.h>
#include <winsock2.h>
#include "MySQL/mysql.h"
#include "CZoneInPacket.h"
#include "CDb.h"
#include "CZone.h"
#include "CZoneHandler.h"
#include "structs.h"

CZoneInPacket::CZoneInPacket(CChar * schar, short csid) {
	memset(this->data, 0, 0x82*2);
	unsigned int  status;
	long count;
	long time1;

	this->type = 0x0A;
	this->size = 0x82;

	count = ((long)time(NULL) - 1009810801);
	time1 = (long)time(NULL);

	memcpy(&this->data[0x38-4], &time1, sizeof(time1));
	memcpy(&this->data[0x3C-4], &count, sizeof(count));

	if(csid != -1) {
		this->data[0x62-4] = (unsigned char)schar->getZone();
		memcpy(&this->data[0x64-4],&csid,2);
	
		this->data[0x68-4] = 0x01;
		if((schar->getZone() >= 0xee) && (schar->getZone() <= 0xee+3)) {
			this->data[0x66-4] = 0x93;
		} else {
			
			this->data[0x66-4] = 0x83;
		}
	}

	memcpy(&this->data[0x44-4], &(schar->l), sizeof(schar->l));
	memcpy(&this->data[0x04-4], &(schar->m_id), sizeof(int));

	unsigned short targId = schar->getTargId();
	memcpy(&this->data[0x08-4], &targId, sizeof(unsigned int));

	this->data[0x1f-4] = schar->getAnimation();
	

	status = ((schar->h.flags & 0xff000000) >> 5) + ((schar->h.flags & 0x000000ff) << 7);
	//Sets the players gender
	status |= (schar->getGender() * 0x0002);

	memcpy(&this->data[0x20-4], &(status), sizeof(int));

	this->data[0x22-4] = schar->h.nameFlag;

	memcpy(&this->data[0x84-4], schar->m_name, 16);

	schar->j.reserved = (unsigned char)(schar->j.unlocked >> 16);
	memcpy(&this->data[0xB8-4], &(schar->j), sizeof(schar->j));

	memcpy(&this->data[0xCC-4], &(schar->stats.str), 28);
	int hp = schar->getHp();
	int mp = schar->getMp();
	memcpy(&this->data[0xE8-4], (char *)&hp, 4);
	memcpy(&this->data[0xEB-4], (char *)&mp, 4);
	schar->h.flags = status;

	this->data[0x29-4] = 0x01;


	this->data[0x2A-4] = 0x80;
	this->data[0x1C-4] = schar->m_speed;
	this->data[0x1D-4] = schar->m_speedAlt;
	this->data[0x1E-4] = 0x64;

	if((schar->getZone() > 0xFC) || ((schar->getZone() > 0xD5) & (schar->getZone() < 0xDB)) ){
		this->data[0x80-4] = 1;										//mog house flag
		this->data[0x30-4] = schar->loc.prevzone;					//previous zone
		this->data[0x42-4] = schar->loc.prevzone;					//previous zone
		this->data[0xAA-4] = (unsigned char)schar->getZone();						//to zone

	} else {
	//	this->data[0x80-4] = 2;											//mog house flag ?!
		this->data[0x30-4] = (unsigned char)schar->getZone();						//to zone
		this->data[0x42-4] = (unsigned char)schar->getZone();						//to zone
	}

	MYSQL_RES * res;
	MYSQL_ROW row;
	char query[1024];
	sprintf(query, "SELECT sng1, sng2, battlesolo, battlemulti FROM zone_settings WHERE Zoneid = 0x%x ", schar->getZone());
	count = (int) strlen(query);

	MYSQL* mysqlConn=CDb::getConn(); //get conn
	
	mysql_real_query(mysqlConn, query, count);

	res = mysql_store_result(mysqlConn);
	CDb::pushConn(mysqlConn); //release conn

	if (res) 
	{
		if(row = mysql_fetch_row(res)) 
		{
			this->data[0x56-4] = atoi(row[0]);	// background music
			this->data[0x58-4] = atoi(row[1]);	// background 2
			this->data[0x5A-4] = atoi(row[2]);	// solo battle music
			this->data[0x5C-4] = atoi(row[3]);	// party battle music
		}
		mysql_free_result(res);
	}
	
	memcpy(&this->data[0x0B-4], &(schar->loc.p.rotation), 1);						//rotation
	memcpy(&this->data[0x0C-4], (char *)&(schar->loc.p.x), sizeof(float));			//x
	memcpy(&this->data[0x10-4], (char *)&(schar->loc.p.y), sizeof(float));			//y
	memcpy(&this->data[0x14-4], (char *)&(schar->loc.p.z), sizeof(float));			//z
	memcpy(&this->data[0x60-4], (char *)&(schar->boundaryId),sizeof(short));

	memcpy(&this->data[0x98-4], &(schar->clientConn.clientIP), 4);

	this->data[0x100-4] = 0x01;

	CZone * zone = CZoneHandler::getInstance()->getZone(schar->getZone());
	
	weather weatherCurrent = zone->getCurrentWeather();
	weather weatherLast = zone->getLastWeather();

	memcpy(&this->data[0x68-4], (unsigned short *)&(weatherCurrent.weatherEffect),sizeof(unsigned short));
	memcpy(&this->data[0x6A-4], (unsigned short *)&(weatherLast.changeTime),sizeof(unsigned short));
	memcpy(&this->data[0x6C-4], (unsigned int *)&(weatherCurrent.weatherEffect),sizeof(unsigned int));
	memcpy(&this->data[0x70-4], (unsigned int *)&(weatherLast.changeTime),sizeof(unsigned int));

	if(schar->onTransport > 0)
		this->data[0x66-4] = 0x03;

	// this flag does the fadein after zoning
	//this->data[0xac-4] = 0x01;


}

CZoneInPacket::~CZoneInPacket() {

}